#include "network.hpp"

Network::Network() { }

void Network::addDevice(const string &name) {
    Device* device = new Device(name);
    this->nameToDevice[name] = device;
}

/**
 * A Link is represented a pair of ports owned by different devices.
 *
 * @param fromd
 * @param fromp
 * @param tod
 * @param top
 */
void Network::addLink(const string &fromd, const string &fromp, const string &tod, const string &top) {
    Device *fromdev = nameToDevice[fromd];
    Device *todev = nameToDevice[tod];
    Port *frompt = fromdev->getPortByName(fromp);
    Port *topt = todev->getPortByName(top);
    if (frompt == nullptr) {
        frompt = fromdev->addPortByName(fromp);
    }
    if (topt == nullptr) {
        topt = todev->addPortByName(top);
    }
    frompt->peers.insert(topt);
    topt->peers.insert(frompt);
}